<?php
declare (strict_types = 1);

namespace app\service\logic\backend\system;

use app\exception\LogicException;
use app\model\communal\RolePermissionsModel;
use Exception;
use think\Collection;
use app\facade\StringFacade;
use app\model\communal\AdminModel;
use think\db\exception\DbException;
use app\model\communal\PermissionsModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

final class PermissionsService
{
    /**
     * amis 下拉菜单无分页
     *
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getOptions()
    {
        $options = $this->getDrop();
        return $this->_getAmisBTree($options);
    }

    /**
     * 无分页列表
     *
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getDrop()
    {
        // 根据公司获取可勾选权限
        return PermissionsModel::field(PermissionsModel::$column)
            ->order(['sort' => 'asc', 'id' => 'asc'])
            ->select()
            ->toArray();
    }

    /**
     * 初始化
     *
     * @return Collection
     * @throws Exception
     */
    public function initData()
    {
        // id 即 权限类型 不可更改
        $init = [
            /*
             * 工作台
             */
            ['id' => '1', 'pid' => '0', 'name' => '工作台', 'front_end' => null, 'api' => null],
                ['id' => '2', 'pid' => '1', 'name' => '全部', 'front_end' => null, 'api' => null],
                    ['id' => '3', 'pid' => '2', 'name' => '工作台', 'front_end' => null, 'api' => null],
                        ['id' => '4', 'pid' => '3', 'name' => '查看', 'front_end' => 'workbench', 'api' => null],

            // /*
            //  * 消息提醒
            //  */
            // ['id' => '1000', 'pid' => '0', 'name' => '消息提醒', 'front_end' => null, 'api' => null],
            //     ['id' => '1001', 'pid' => '1000', 'name' => '全部', 'front_end' => null, 'api' => null],
            //         ['id' => '1002', 'pid' => '1001', 'name' => '消息列表', 'front_end' => null, 'api' => null],
            //             ['id' => '1003', 'pid' => '1002', 'name' => '查看', 'front_end' => 'message,message_list,message_list_read', 'api' => null],
            //
            // /*
            //  * 订单管理
            //  */
            // ['id' => '2000', 'pid' => '0', 'name' => '订单管理', 'front_end' => null, 'api' => null],
            //     ['id' => '2001', 'pid' => '2000', 'name' => '房源销售订单', 'front_end' => null, 'api' => null],
            //         ['id' => '2002', 'pid' => '2001', 'name' => '房源销售订单', 'front_end' => null, 'api' => null],
            //             ['id' => '2003', 'pid' => '2002', 'name' => '查看', 'front_end' => 'order,order_housing_sales,order_housing_sales_read', 'api' => null],
            //             ['id' => '2004', 'pid' => '2002', 'name' => '新增', 'front_end' => 'order_housing_sales_save', 'api' => null],
            //             ['id' => '2005', 'pid' => '2002', 'name' => '编辑', 'front_end' => 'order_housing_sales_update', 'api' => null],
            //     ['id' => '2100', 'pid' => '2000', 'name' => '内部订单', 'front_end' => null, 'api' => null],
            //         ['id' => '2101', 'pid' => '2100', 'name' => '业主回家', 'front_end' => null, 'api' => null],
            //             ['id' => '2102', 'pid' => '2101', 'name' => '查看', 'front_end' => 'order,order_internal,order_internal_return,order_internal_return_read', 'api' => null],
            //             ['id' => '2103', 'pid' => '2101', 'name' => '新增', 'front_end' => 'order_internal_return_save', 'api' => null],
            //             ['id' => '2104', 'pid' => '2101', 'name' => '编辑', 'front_end' => 'order_internal_return_update', 'api' => null],
            //         ['id' => '2110', 'pid' => '2100', 'name' => '机场接送', 'front_end' => null, 'api' => null],
            //             ['id' => '2111', 'pid' => '2110', 'name' => '查看', 'front_end' => 'order,order_internal,order_internal_pick,order_internal_pick_read', 'api' => null],
            //             ['id' => '2112', 'pid' => '2110', 'name' => '新增', 'front_end' => 'order_internal_pick_save', 'api' => null],
            //             ['id' => '2113', 'pid' => '2110', 'name' => '编辑', 'front_end' => 'order_internal_pick_update', 'api' => null],
            //
            // /*
            //  * 收益管理
            //  */
            // ['id' => '3000', 'pid' => '0', 'name' => '收益管理', 'front_end' => null, 'api' => null],
            //     ['id' => '3001', 'pid' => '3000', 'name' => '提现记录', 'front_end' => null, 'api' => null],
            //         ['id' => '3002', 'pid' => '3001', 'name' => '提现记录', 'front_end' => null, 'api' => null],
            //             ['id' => '3003', 'pid' => '3002', 'name' => '查看', 'front_end' => 'income,income_cash_record,income_cash_record_read', 'api' => null],
            //             ['id' => '3004', 'pid' => '3002', 'name' => '编辑', 'front_end' => 'income_cash_record_update', 'api' => null],
            //
            // /*
            //  * 工单服务中心
            //  */
            // ['id' => '5000', 'pid' => '0', 'name' => '工单服务中心', 'front_end' => null, 'api' => null],
            //     ['id' => '5001', 'pid' => '5000', 'name' => '房源维保', 'front_end' => null, 'api' => null],
            //         ['id' => '5002', 'pid' => '5001', 'name' => '通风除尘', 'front_end' => null, 'api' => null],
            //             ['id' => '5003', 'pid' => '5002', 'name' => '查看', 'front_end' => 'work,work_housing,work_housing_air,work_housing_air_read', 'api' => null],
            //             ['id' => '5004', 'pid' => '5002', 'name' => '新增', 'front_end' => 'work_housing_air_save', 'api' => null],
            //             ['id' => '5005', 'pid' => '5002', 'name' => '编辑', 'front_end' => 'work_housing_air_update', 'api' => null],
            //         ['id' => '5100', 'pid' => '5001', 'name' => '全屋保洁', 'front_end' => null, 'api' => null],
            //             ['id' => '5101', 'pid' => '5100', 'name' => '查看', 'front_end' => 'work,work_housing,work_housing_all,work_housing_all_read', 'api' => null],
            //             ['id' => '5102', 'pid' => '5100', 'name' => '新增', 'front_end' => 'work_housing_all_save', 'api' => null],
            //             ['id' => '5103', 'pid' => '5100', 'name' => '编辑', 'front_end' => 'work_housing_all_update', 'api' => null],
            //         ['id' => '5200', 'pid' => '5001', 'name' => '焕新服务', 'front_end' => null, 'api' => null],
            //             ['id' => '5201', 'pid' => '5200', 'name' => '查看', 'front_end' => 'work,work_housing,work_housing_new,work_housing_new_read', 'api' => null],
            //             ['id' => '5202', 'pid' => '5200', 'name' => '新增', 'front_end' => 'work_housing_new_save', 'api' => null],
            //         ['id' => '5300', 'pid' => '5001', 'name' => '退房打扫布草', 'front_end' => null, 'api' => null],
            //             ['id' => '5301', 'pid' => '5300', 'name' => '查看', 'front_end' => 'work,work_housing,work_housing_out,work_housing_out_read', 'api' => null],
            //             ['id' => '5302', 'pid' => '5300', 'name' => '新增', 'front_end' => 'work_housing_out_save', 'api' => null],
            //
            // /*
            //  * 业主管理
            //  */
            // ['id' => '6000', 'pid' => '0', 'name' => '业主管理', 'front_end' => null, 'api' => null],
            //     ['id' => '6001', 'pid' => '6000', 'name' => '业主列表', 'front_end' => null, 'api' => null],
            //         ['id' => '6002', 'pid' => '6001', 'name' => '业主列表', 'front_end' => null, 'api' => null],
            //             ['id' => '6003', 'pid' => '6002', 'name' => '查看', 'front_end' => 'owner,owner_list,owner_list_read', 'api' => null],
            //             ['id' => '6004', 'pid' => '6002', 'name' => '新增', 'front_end' => 'owner_list_save', 'api' => null],
            //             ['id' => '6005', 'pid' => '6002', 'name' => '编辑', 'front_end' => 'owner_list_update', 'api' => null],
            //         ['id' => '6100', 'pid' => '6001', 'name' => '银行卡变更', 'front_end' => null, 'api' => null],
            //             ['id' => '6101', 'pid' => '6100', 'name' => '查看', 'front_end' => 'owner,owner_banks_check,owner_banks_check_read', 'api' => null],
            //             // ['id' => '6102', 'pid' => '6100', 'name' => '新增', 'front_end' => 'owner_banks_check_save', 'api' => null],
            //             ['id' => '6103', 'pid' => '6100', 'name' => '编辑', 'front_end' => 'owner_banks_check_update', 'api' => null],
            //
            // /*
            //  * 保洁管理
            //  */
            // ['id' => '6500', 'pid' => '0', 'name' => '保洁管理', 'front_end' => null, 'api' => null],
            //     ['id' => '6501', 'pid' => '6500', 'name' => '保洁列表', 'front_end' => null, 'api' => null],
            //         ['id' => '6502', 'pid' => '6501', 'name' => '保洁列表', 'front_end' => null, 'api' => null],
            //             ['id' => '6503', 'pid' => '6502', 'name' => '查看', 'front_end' => 'cleaning,cleaning_list,cleaning_list_read', 'api' => null],
            //             ['id' => '6504', 'pid' => '6502', 'name' => '新增', 'front_end' => 'cleaning_list_save', 'api' => null],
            //             ['id' => '6505', 'pid' => '6502', 'name' => '编辑', 'front_end' => 'cleaning_list_update', 'api' => null],
            //
            // /*
            //  * 基础信息维护
            //  */
            // ['id' => '7000', 'pid' => '0', 'name' => '基础信息维护', 'front_end' => null, 'api' => null],
            //     ['id' => '7001', 'pid' => '7000', 'name' => '房源管理', 'front_end' => null, 'api' => null],
            //         ['id' => '7002', 'pid' => '7001', 'name' => '房源列表', 'front_end' => null, 'api' => null],
            //             ['id' => '7003', 'pid' => '7002', 'name' => '查看', 'front_end' => 'base,base_housing,base_housing_list,base_housing_list_read', 'api' => null],
            //             ['id' => '7004', 'pid' => '7002', 'name' => '新增', 'front_end' => 'base_housing_list_save', 'api' => null],
            //             ['id' => '7005', 'pid' => '7002', 'name' => '编辑', 'front_end' => 'base_housing_list_update', 'api' => null],
            //         ['id' => '7100', 'pid' => '7001', 'name' => '小区维护', 'front_end' => null, 'api' => null],
            //             ['id' => '7101', 'pid' => '7100', 'name' => '查看', 'front_end' => 'base,base_housing,base_housing_estate,base_housing_estate_read', 'api' => null],
            //             ['id' => '7102', 'pid' => '7100', 'name' => '新增', 'front_end' => 'base_housing_estate_save', 'api' => null],
            //             ['id' => '7103', 'pid' => '7100', 'name' => '编辑', 'front_end' => 'base_housing_estate_update', 'api' => null],
            //             ['id' => '7104', 'pid' => '7100', 'name' => '删除', 'front_end' => 'base_housing_estate_delete', 'api' => null],
            //         ['id' => '7200', 'pid' => '7001', 'name' => '楼号维护', 'front_end' => null, 'api' => null],
            //             ['id' => '7201', 'pid' => '7200', 'name' => '查看', 'front_end' => 'base,base_housing,base_housing_number,base_housing_number_read', 'api' => null],
            //             ['id' => '7202', 'pid' => '7200', 'name' => '新增', 'front_end' => 'base_housing_number_save', 'api' => null],
            //             ['id' => '7203', 'pid' => '7200', 'name' => '编辑', 'front_end' => 'base_housing_number_update', 'api' => null],
            //             ['id' => '7204', 'pid' => '7200', 'name' => '删除', 'front_end' => 'base_housing_number_delete', 'api' => null],
            //         ['id' => '7300', 'pid' => '7001', 'name' => '单元维护', 'front_end' => null, 'api' => null],
            //             ['id' => '7301', 'pid' => '7300', 'name' => '查看', 'front_end' => 'base,base_housing,base_housing_unit,base_housing_unit_read', 'api' => null],
            //             ['id' => '7302', 'pid' => '7300', 'name' => '新增', 'front_end' => 'base_housing_unit_save', 'api' => null],
            //             ['id' => '7303', 'pid' => '7300', 'name' => '编辑', 'front_end' => 'base_housing_unit_update', 'api' => null],
            //             ['id' => '7304', 'pid' => '7300', 'name' => '删除', 'front_end' => 'base_housing_unit_delete', 'api' => null],
            //     ['id' => '7400', 'pid' => '7000', 'name' => '权益管理', 'front_end' => null, 'api' => null],
            //         ['id' => '7401', 'pid' => '7400', 'name' => '接送权益', 'front_end' => null, 'api' => null],
            //             ['id' => '7402', 'pid' => '7401', 'name' => '查看', 'front_end' => 'base,base_rights,base_rights_pick,base_rights_pick_read', 'api' => null],
            //             ['id' => '7403', 'pid' => '7401', 'name' => '新增', 'front_end' => 'base_rights_pick_save', 'api' => null],
            //             ['id' => '7404', 'pid' => '7401', 'name' => '编辑', 'front_end' => 'base_rights_pick_update', 'api' => null],
            //         ['id' => '7500', 'pid' => '7400', 'name' => '通风除尘权益', 'front_end' => null, 'api' => null],
            //             ['id' => '7501', 'pid' => '7500', 'name' => '查看', 'front_end' => 'base,base_rights,base_rights_cleaning,base_rights_cleaning_read', 'api' => null],
            //             ['id' => '7502', 'pid' => '7500', 'name' => '新增', 'front_end' => 'base_rights_cleaning_save', 'api' => null],
            //             ['id' => '7503', 'pid' => '7500', 'name' => '编辑', 'front_end' => 'base_rights_cleaning_update', 'api' => null],
            //         ['id' => '7600', 'pid' => '7400', 'name' => '全屋保洁权益', 'front_end' => null, 'api' => null],
            //             ['id' => '7601', 'pid' => '7600', 'name' => '查看', 'front_end' => 'base,base_rights,base_rights_cleaning_all,base_rights_cleaning_all_read', 'api' => null],
            //             ['id' => '7602', 'pid' => '7600', 'name' => '新增', 'front_end' => 'base_rights_cleaning_all_save', 'api' => null],
            //             ['id' => '7603', 'pid' => '7600', 'name' => '编辑', 'front_end' => 'base_rights_cleaning_all_update', 'api' => null],

            /*
             * 量表管理
             */
            ['id' => '70000', 'pid' => '0', 'name' => '量表管理', 'front_end' => null, 'api' => null],
                ['id' => '70001', 'pid' => '70000', 'name' => '量表分类', 'front_end' => null, 'api' => null],
                    ['id' => '70002', 'pid' => '70001', 'name' => '查看', 'front_end' => 'scale,scale_category,scale_category_read', 'api' => null],
                    ['id' => '70003', 'pid' => '70001', 'name' => '新增', 'front_end' => 'scale_category_save', 'api' => null],
                    ['id' => '70004', 'pid' => '70001', 'name' => '编辑', 'front_end' => 'scale_category_update', 'api' => null],

            /*
             * 维护管理
             */
            ['id' => '90000', 'pid' => '0', 'name' => '维护管理', 'front_end' => null, 'api' => null],
                // ['id' => '8002', 'pid' => '8001', 'name' => '全屋房型保洁维护', 'front_end' => null, 'api' => null],
                //     ['id' => '8003', 'pid' => '8002', 'name' => '查看', 'front_end' => 'maintain,maintain_layout,maintain_layout_read', 'api' => null],
                //     ['id' => '8004', 'pid' => '8002', 'name' => '新增', 'front_end' => 'maintain_layout_save', 'api' => null],
                //     ['id' => '8005', 'pid' => '8002', 'name' => '编辑', 'front_end' => 'maintain_layout_update', 'api' => null],
                // ['id' => '8100', 'pid' => '8001', 'name' => '保洁公司维护', 'front_end' => null, 'api' => null],
                //     ['id' => '8101', 'pid' => '8100', 'name' => '查看', 'front_end' => 'maintain,maintain_cleaning_company,maintain_cleaning_company_read', 'api' => null],
                //     ['id' => '8102', 'pid' => '8100', 'name' => '新增', 'front_end' => 'maintain_cleaning_company_save', 'api' => null],
                //     ['id' => '8103', 'pid' => '8100', 'name' => '编辑', 'front_end' => 'maintain_cleaning_company_update', 'api' => null],
                // ['id' => '8200', 'pid' => '8001', 'name' => '常见问题', 'front_end' => null, 'api' => null],
                //     ['id' => '8201', 'pid' => '8200', 'name' => '查看', 'front_end' => 'maintain,maintain_qa,maintain_qa_read', 'api' => null],
                //     ['id' => '8202', 'pid' => '8200', 'name' => '新增', 'front_end' => 'maintain_qa_save', 'api' => null],
                //     ['id' => '8203', 'pid' => '8200', 'name' => '编辑', 'front_end' => 'maintain_qa_update', 'api' => null],
                // ['id' => '8300', 'pid' => '8001', 'name' => '权益说明', 'front_end' => null, 'api' => null],
                //     ['id' => '8301', 'pid' => '8300', 'name' => '查看', 'front_end' => 'maintain,maintain_rights,maintain_rights_read', 'api' => null],
                ['id' => '90001', 'pid' => '90000', 'name' => '广告位维护', 'front_end' => null, 'api' => null],
                    ['id' => '90002', 'pid' => '90001', 'name' => '查看', 'front_end' => 'maintain,maintain_banner,maintain_banner_read', 'api' => null],
                    ['id' => '90003', 'pid' => '90001', 'name' => '新增', 'front_end' => 'maintain_banner_save', 'api' => null],
                    ['id' => '90004', 'pid' => '90001', 'name' => '编辑', 'front_end' => 'maintain_banner_update', 'api' => null],
                ['id' => '90100', 'pid' => '90000', 'name' => '广告位维护', 'front_end' => null, 'api' => null],
                    ['id' => '90101', 'pid' => '90100', 'name' => '查看', 'front_end' => 'maintain,maintain_placard,maintain_placard_read', 'api' => null],
                    ['id' => '90102', 'pid' => '90100', 'name' => '新增', 'front_end' => 'maintain_placard_save', 'api' => null],
                    ['id' => '90103', 'pid' => '90100', 'name' => '编辑', 'front_end' => 'maintain_placard_update', 'api' => null],

            /*
             * 系统管理
             */
            ['id' => '100000', 'pid' => '0', 'name' => '系统管理', 'front_end' => null, 'api' => null],
                ['id' => '100001', 'pid' => '100000', 'name' => '角色权限', 'front_end' => null, 'api' => null],
                    ['id' => '100002', 'pid' => '100001', 'name' => '查看', 'front_end' => 'system,system_role,system_role_read', 'api' => null],
                    ['id' => '100003', 'pid' => '100001', 'name' => '新增', 'front_end' => 'system_role_save', 'api' => null],
                    ['id' => '100004', 'pid' => '100001', 'name' => '编辑', 'front_end' => 'system_role_update', 'api' => null],
                    ['id' => '100005', 'pid' => '100001', 'name' => '删除', 'front_end' => 'system_role_delete', 'api' => null],
                ['id' => '100100', 'pid' => '100000', 'name' => '组织架构', 'front_end' => null, 'api' => null],
                    ['id' => '100101', 'pid' => '100100', 'name' => '查看', 'front_end' => 'system,system_admin,system_admin_read', 'api' => null],
                    ['id' => '100102', 'pid' => '100100', 'name' => '新增', 'front_end' => 'system_admin_save', 'api' => null],
                    ['id' => '100103', 'pid' => '100100', 'name' => '编辑', 'front_end' => 'system_admin_update', 'api' => null],
        ];

        $exitIds = PermissionsModel::where(1,1)->column('id');
        $inserts = [];
        foreach ($init as $item) {
            if (in_array($item['id'], $exitIds)) {
                continue ;
            }

            $inserts[] = $item;
        }
        if (!empty($inserts)) {
            return (new PermissionsModel)->saveAll($inserts, false);
        } else {
            return new Collection();
        }
    }

    /**
     * 获取员工前端标识
     *
     * @return mixed
     * @throws LogicException
     */
    public function getFrontEndByAuth()
    {
        $admin = request()->middleware('admin');
        // 超管不控制权限
        if (AdminModel::IS_SUPER_ONE == $admin['is_super']) {
            $frontEnd = PermissionsModel::whereNotNull('front_end')
                ->column('front_end');
        } else {
            // 获取角色权限
            $role = $admin->role()->findOrEmpty();
            if (!$role->isEmpty()) {
                $frontEnd = $role->belongsToManyPermissions
                    ->column('front_end');
            } else {
                $frontEnd = [];
            }
        }

        return StringFacade::explodeImplode(array_merge($frontEnd, PermissionsModel::$frontEndWhiteList));
    }

    /**
     * 获取具有报价通知的钉钉用户ID
     *
     * @param string $frontend
     * @return string
     */
    public function getDingUserIdByFrontend($frontend = 'price_internal_notify')
    {
        $permitsId = PermissionsModel::where('front_end', $frontend)->value('id');
        $roleIds = RolePermissionsModel::where('permissions_id', $permitsId)->column('role_id');
        $userIds = AdminModel::whereIn('role_id', $roleIds)->column('user_id');
        return empty($userIds) ? '' : implode(',', $userIds);
    }

    /**
     * 获取权限树状
     *
     * @param $list
     * @param int $pid
     * @param array $permissionsIds
     * @return array
     */
    private function _getAmisBTree($list, $pid = 0, $permissionsIds = [])
    {
        $result = [];
        foreach ($list as $item) {
            if ($item['pid'] == $pid) {
                $temp = [
                    'label' => $item['name'],
                    'value' => $item['id'],
                ];
                $temp['children'] = $this->_getAmisBTree($list, $item['id'], $permissionsIds);
                $result[] = $temp;
            }
        }
        return $result;
    }
}
